package com.app.studyroom;

import android.os.Bundle;
import android.widget.ImageView;
import android.widget.Toast;

import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import com.app.studyroom.adapter.ContactAdminAdapter;
import com.app.studyroom.api.ApiClient;
import com.app.studyroom.api.ApiResponse;
import com.app.studyroom.api.ApiService;
import com.app.studyroom.db.UserInfo;

import java.util.ArrayList;
import java.util.List;

import retrofit2.Call;
import retrofit2.Callback;
import retrofit2.Response;

public class ContactAdminActivity extends BaseActivity {

    private ImageView Image_back;
    private RecyclerView recyclerView;
    private ContactAdminAdapter adapter;
    private List<UserInfo> adminList = new ArrayList<>();

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_contact_admin);

        Image_back = findViewById(R.id.ic_back);
        Image_back.setOnClickListener(v -> onBackPressed());

        recyclerView = findViewById(R.id.recyclerContactAdmin);
        recyclerView.setLayoutManager(new LinearLayoutManager(this));
        adapter = new ContactAdminAdapter(adminList);
        recyclerView.setAdapter(adapter);

        loadAllUsers();
    }

    private void loadAllUsers() {
        ApiService apiService = ApiClient.getClient().create(ApiService.class);
        apiService.getAllUserAdmin().enqueue(new Callback<ApiResponse<List<UserInfo>>>() {
            @Override
            public void onResponse(Call<ApiResponse<List<UserInfo>>> call, Response<ApiResponse<List<UserInfo>>> response) {
                if (response.isSuccessful() && response.body() != null && response.body().isSuccess()) {
                    List<UserInfo> userList = response.body().getData();

                    adminList.clear();
                    for (UserInfo user : userList) {
                        if ("管理员".equals(user.getUserBs()) || "超级管理员".equals(user.getUserBs()) || "区域管理员".equals(user.getUserBs())) {
                            adminList.add(user);
                        }
                    }
                    adapter.notifyDataSetChanged();
                } else {
                    Toast.makeText(getApplicationContext(), "加载失败：" + (response.body() != null ? response.body().getMessage() : "未知错误"), Toast.LENGTH_SHORT).show();
                }
            }

            @Override
            public void onFailure(Call<ApiResponse<List<UserInfo>>> call, Throwable t) {
                Toast.makeText(getApplicationContext(), "网络异常：" + t.getMessage(), Toast.LENGTH_SHORT).show();
            }
        });
    }
}
